package com.frame.work.frame.deal.device0fac;

import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.frame.deal.base.BaseFrame;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 86地暖面板
 */
@Data
public class DiNuanMianBan_0FAC0302 extends BaseFrame {

    private String switchState;

    public DiNuanMianBan_0FAC0302(String gatewayMac, String data) {

        //将基础数据进行处理
        super(gatewayMac, data);

        //处理此设备特有的数据
        this.setSwitchState(StringUtils.substring(data, data.length() - 2, data.length()));

        String lock = StringUtils.substring(data, data.length() - 14, data.length() - 12);//锁定状态
        String actualTemp = StringUtils.substring(data, data.length() - 12, data.length() - 8);//环境温度
        float actualTemp1 = Float.parseFloat(StringUtils.substring(actualTemp, 2, 4) + '.' + StringUtils.substring(actualTemp, 0, 2));
        String temperature = StringUtils.substring(data, data.length() - 4, data.length());//温度设置
        //四舍五入
        float temperature2 = Float.parseFloat(StringUtils.substring(temperature, 2, 4) + '.' + StringUtils.substring(temperature, 0, 2));
        DecimalFormat df = new DecimalFormat(".0");
        String temperature1 = df.format(temperature2);
        String position = "";
        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("switchState", switchState);
        actions.put("lock", lock);
        actions.put("temperature", String.valueOf(temperature1));
        actions.put("position", position);
        actions.put("actualTemp", String.valueOf(actualTemp1));
        this.setActions(actions);

        //处理好的json对象
        String json = JSONObject.toJSONString(this);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, json);
    }

}
